package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserVerifyRequest;
import com.cskaoyan.user.dto.UserVerifyResponse;
import com.cskaoyan.user.service.UserVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author sosog
 * @version 1.0.0
 * @description TODO
 * @date 2022/6/17 16:04
 */
@Slf4j
@Service
public class UserVerifyServiceImpl implements UserVerifyService {

    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            // 先做参数校验
            request.requestCheck();
            // 根据uuid查出未过期的需要verify的注册用户信息
            Example selectExample = new Example(UserVerify.class);
            selectExample.createCriteria()
                    .andEqualTo("uuid", request.getUuid())
                    .andEqualTo("isExpire", "N");
            List<UserVerify> userVerifies = userVerifyMapper.selectByExample(selectExample);
            if (userVerifies == null || userVerifies.size() == 0) {
                // 如果没有查到则数据不存在
                response.setCode(UserRetCode.DATA_NOT_EXIST.getCode());
                response.setMsg(UserRetCode.DATA_NOT_EXIST.getMessage());
                return response;
            }
            if (userVerifies.size() > 1) {
                // 如果查到了多条则数据重复
                response.setCode(UserRetCode.DATA_REPEATED.getCode());
                response.setMsg(UserRetCode.DATA_REPEATED.getMessage());
                return response;
            }
            // 拿到需要verify的用户信息
            UserVerify user = userVerifies.get(0);
            // 如果is_verify为Y，则返回提示信息
            if ("Y".equals(user.getIsVerify())) {
                response.setCode(UserRetCode.ALREADY_VERIFIED.getCode());
                response.setMsg(UserRetCode.ALREADY_VERIFIED.getMessage());
                return response;
            }
            // 检查用户的注册信息是否过期【超过一天则过期】
            if (System.currentTimeMillis() - user.getRegisterDate().getTime() > 60000) {
                // 已过期则将is_expire置为Y，并更新，且抛出用户注册信息非法给前端
                user.setIsExpire("Y");
                userVerifyMapper.updateByPrimaryKeySelective(user);
                response.setCode(UserRetCode.USER_VERIFY_INFO_EXPIRED.getCode());
                response.setMsg(UserRetCode.USER_VERIFY_INFO_EXPIRED.getMessage());
                return response;
            }
            // 未过期则同时更新tb_user_verify和tb_member中的is_verify字段为Y
            user.setIsVerify("Y");
            userVerifyMapper.updateByPrimaryKeySelective(user);
            Member member = new Member();
            member.setIsVerified("Y");
            member.setUsername(user.getUsername());
            Example example = new Example(Member.class);
            example.createCriteria().andEqualTo("username", member.getUsername());
            memberMapper.updateByExampleSelective(member,example);
            // 设置通用成功代码
            response.setCode(UserRetCode.SUCCESS.getCode());
            response.setMsg(UserRetCode.SUCCESS.getMessage());
            return response;
        } catch (Exception e) {
            log.error("UserVerifyServiceImpl.verify occur Exception :"+e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
/*
@Slf4j
@Service
public class UserVerifyServiceImpl implements UserVerifyService {

    @Autowired
    UserVerifyMapper userVerifyMapper;


    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            // 先做参数校验
            request.requestCheck();
            // 然后根据username和uuid将tb_user_verify表中的is_verify置为true
            Example example = new Example(UserVerify.class);
            example.createCriteria().andEqualTo("uuid", request.getUuid());
            UserVerify userVerify = new UserVerify();
            userVerify.setIsVerify("Y");
            int updates = userVerifyMapper.updateByExampleSelective(userVerify, example);
            if (updates == 1) {
                // 设置通用成功代码
                response.setCode(UserRetCode.SUCCESS.getCode());
                response.setMsg(UserRetCode.SUCCESS.getMessage());
            } else if (updates == 0) {
                // 如果没有修改则数据不存在
                response.setCode(UserRetCode.DATA_NOT_EXIST.getCode());
                response.setMsg(UserRetCode.DATA_NOT_EXIST.getMessage());
            } else {
                // 如果修改了多条则数据重复
                response.setCode(UserRetCode.DATA_REPEATED.getCode());
                response.setMsg(UserRetCode.DATA_REPEATED.getMessage());
            }

        } catch (Exception e) {
            // 记录日志
            log.error("UserVerifyServiceImpl.verify occur Exception :"+e);
            // 设置回显前端的失败代码和信息
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
 */
